package cn.unknowpupil.init.utils;

import cn.hutool.core.util.IdUtil;
import cn.unknowpupil.init.common.enums.LoginCodeEnum;
import cn.unknowpupil.init.domain.dto.LoginCode;
import cn.unknowpupil.init.exceptions.HealthException;
import cn.unknowpupil.init.properties.SecurityProperties;
import cn.unknowpupil.init.utils.redis.RedisUtils;
import cn.unknowpupil.init.utils.strings.StringUtils;
import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.ChineseCaptcha;
import com.wf.captcha.ChineseGifCaptcha;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Created at 2020/10/20 by w10g <br>
 * Initialize CaptchaUtil ... <br>
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Data
@Component
public class CaptchaUtils extends com.wf.captcha.utils.CaptchaUtil {
    private LoginCode loginCode;

    @Autowired
    private SecurityProperties securityProperties;

    @Autowired
    private RedisUtils redisUtil;

    /**
     * 获取验证码生产类
     *
     * @return /
     */
    public Captcha getCaptcha() {
        if (Objects.isNull(loginCode)) {
            loginCode = new LoginCode();
            if (Objects.isNull(loginCode.getCodeType())) {
                loginCode.setCodeType(LoginCodeEnum.arithmetic);
            }
        }
        return switchCaptcha(loginCode);
    }

    /**
     * 依据配置信息生产验证码
     *
     * @param loginCode 验证码配置信息
     * @return /
     */
    private Captcha switchCaptcha(LoginCode loginCode) {
        Captcha captcha;

        switch (loginCode.getCodeType()) {
            case arithmetic:
                // 算术类型 https://gitee.com/whvse/EasyCaptcha
                captcha = new ArithmeticCaptcha(loginCode.getWidth(), loginCode.getHeight());
                // 几位数运算，默认是两位
                captcha.setLen(loginCode.getLength());
                break;
            case chinese:
                captcha = new ChineseCaptcha(loginCode.getWidth(), loginCode.getHeight());
                captcha.setLen(loginCode.getLength());
                break;
            case chinese_gif:
                captcha = new ChineseGifCaptcha(loginCode.getWidth(), loginCode.getHeight());
                captcha.setLen(loginCode.getLength());
                break;
            case gif:
                captcha = new GifCaptcha(loginCode.getWidth(), loginCode.getHeight());
                captcha.setLen(loginCode.getLength());
                break;
            case spec:
                captcha = new SpecCaptcha(loginCode.getWidth(), loginCode.getHeight());
                captcha.setLen(loginCode.getLength());
                break;
            default:
                throw new HealthException("验证码配置信息错误！正确配置查看 LoginCodeEnum ");
        }

        if (StringUtils.isNotBlank(loginCode.getFontName())) {
            captcha.setFont(new Font(loginCode.getFontName(), Font.PLAIN, loginCode.getFontSize()));
        }
        return captcha;
    }

    public Map<String, Object> getCaptchaCode() {
        Captcha captcha = this.getCaptcha();
        String uuid = IdUtil.simpleUUID();
        String key = securityProperties.getCaptchaCodeKey() + uuid;
        // 保存
        redisUtil.set(key, captcha.text(), loginCode.getExpiration(), TimeUnit.MINUTES);
        // 验证码信息
        Map<String, Object> imgResult = new HashMap<String, Object>(2) {{
            put("img", captcha.toBase64());
            put("uuid", uuid);
        }};
        return imgResult;
    }
}
